//
//  ViewController.m
//  10-homeWork
//
//  Created by 402030900@qq.com on 16/7/27.
//  Copyright © 2016年 402030900@qq.com. All rights reserved.
//

#import "ViewController.h"
#import "LTTableViewCell.h"
#import "UIImageView+WebCache.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UIScrollView * scrollView;
@property(nonatomic,strong)UITableView * tableView;
@property(nonatomic,strong)NSMutableArray * titleArray;
@property(nonatomic,strong)NSMutableArray * picArray;
@property(nonatomic,strong)NSMutableArray * scrollPicArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    [self createData];
    [self createHeaderView];

          // Do any additional setup after loading the view, typically from a nib.
}
-(void)createHeaderView{
    // scrollView
    UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 200)];
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 375, 200)];
    _scrollView.contentSize = CGSizeMake(375 * _scrollPicArray.count, 200);
    _scrollView.backgroundColor = [UIColor cyanColor];
    [headerView addSubview:_scrollView];
    // tableView
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 667 - 20) style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[LTTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    
    _tableView.rowHeight = 100;
    
    // buttonView
    UIView * buttonView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, 375, 100)];
    UIButton * loanButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    UILabel * loanLabel = [[UILabel alloc] init];
    UIButton * directSelingButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    UILabel * directSelingLabel = [[UILabel alloc] init];
    UIButton * lowPriceButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    UILabel * lowPriceLabel = [[UILabel alloc]init];
    UIButton * usedCarButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    UILabel * usedCarLabel = [[UILabel alloc]init];
    
    loanLabel.text = @"贷款买车";
    directSelingLabel.text = @"直销车型";
    lowPriceLabel.text = @"底价买车";
    usedCarLabel.text = @"二手车";
    
    [loanButton setFrame:CGRectMake(20, 10, 50, 50)];
    [loanButton setBackgroundImage:[UIImage imageNamed:@"1"] forState:(UIControlStateNormal)];
    [directSelingButton setFrame:CGRectMake(110, 10, 50, 50)];
    [directSelingButton setBackgroundImage:[UIImage imageNamed:@"2"] forState:(UIControlStateNormal)];
    [lowPriceButton setFrame:CGRectMake(200, 10, 50, 50)];
    [lowPriceButton setBackgroundImage:[UIImage imageNamed:@"3"] forState:(UIControlStateNormal)];
    [usedCarButton setFrame:CGRectMake(290, 10, 50, 50)];
    [usedCarButton setBackgroundImage:[UIImage imageNamed:@"4"] forState:(UIControlStateNormal)];
    
    [loanLabel setFrame:CGRectMake(18, 60, 80, 30)];
    [directSelingLabel setFrame:CGRectMake(108, 60, 80, 30)];
    [lowPriceLabel setFrame:CGRectMake(198, 60, 80, 30)];
    [usedCarLabel setFrame:CGRectMake(288, 60, 80, 30)];
    
    [buttonView addSubview:loanButton];
    [buttonView addSubview:loanLabel];
    [buttonView addSubview:directSelingButton];
    [buttonView addSubview:directSelingLabel];
    [buttonView addSubview:lowPriceButton];
    [buttonView addSubview:lowPriceLabel];
    [buttonView addSubview:usedCarButton];
    [buttonView addSubview:usedCarLabel];
    
    UIView * bothView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375, 200 + 100)];
    [bothView addSubview:buttonView];
    [bothView addSubview:_scrollView];
    
    _tableView.tableHeaderView = bothView;//设为头视图


}
-(void)createData{

    // 数据部分
    NSString * path =[[NSBundle mainBundle]pathForResource:@"YiChe" ofType:@"plist"];
    NSDictionary * resourceDic = [NSDictionary dictionaryWithContentsOfFile:path];
    NSMutableDictionary * dataDic = [NSMutableDictionary dictionary];
    _titleArray = [NSMutableArray array];
//    NSLog(@"%@",resourceDic);
    dataDic = [resourceDic objectForKey:@"data"];
    //    NSLog(@"%@",dataDic);
    NSMutableArray * listArray = [dataDic objectForKey:@"list"];
    //    NSLog(@"%@",listArray);
    // rightLabel数据
    for (NSDictionary * dic in listArray) {
        NSString * titleString  =  [dic objectForKey:@"title"];
        //        NSLog(@"%@",titleString);
        [_titleArray addObject:titleString];
        //        NSLog(@"%@",_titleArray);
    }
    // leftImageView 数据
    _picArray = [NSMutableArray array];
    for (NSDictionary * dic in listArray) {
        NSString * picString =  [dic objectForKey:@"picCover"];
        [_picArray addObject:picString];
//        NSLog(@"%@",_picArray);
    
    }
    //scrollView 数据
   NSArray * cycleImageArr = [dataDic objectForKey:@"cycleImage"];
//    NSLog(@"%@",cycleImageArr);
    _scrollPicArray = [NSMutableArray array];
    // scrollView 放图片
   UIImageView * scrollImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 375, 200)];
    [_scrollView addSubview:scrollImageView];
    
    for (NSDictionary * dic in cycleImageArr) {
    NSString *scrollPicCover = [dic objectForKey:@"picCover"];
        [_scrollPicArray addObject:scrollPicCover];
        [scrollImageView sd_setImageWithURL:[NSURL URLWithString:scrollPicCover]];
    }
    NSLog(@"%@",_scrollPicArray);
  

   
    
    
    
    
}




-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _titleArray.count;
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LTTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.LeftImageView.backgroundColor = [UIColor redColor];
    cell.rightLabel.backgroundColor = [UIColor greenColor];
    cell.rightLabel.text = _titleArray[indexPath.row];
    // 图片
    NSString * picPath = _picArray[indexPath.row];
    [cell.LeftImageView sd_setImageWithURL:[NSURL URLWithString:picPath]];
    cell.backgroundColor = [UIColor yellowColor];

    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
